﻿$.fn.DocReady = function () {
    $("#elfinder").find("dev").removeClass("ui-corner-all");
}
$(function () {
    var myCommands = elFinder.prototype._options.commands;
    var disabled = ['extract', 'archive', 'resize', 'help', 'select'];
    $.each(disabled, function (i, cmd) {
        (idx = $.inArray(cmd, myCommands)) !== -1 && myCommands.splice(idx, 1);
    });
    var selectedFile = null;
    var options = {
        url: 'connector',
        commands: myCommands,
        lang: uiLanguage,
        uiOptions: {
            toolbar: [
		                ['back', 'forward'],
                        ['reload'],
                        ['home', 'up'],
		                ['mkdir', 'mkfile', 'upload'],
		                ['open', 'download'],
		                ['info'],
		                ['quicklook'],
		                ['copy', 'cut', 'paste'],
		                ['rm'],
		                ['duplicate', 'rename', 'edit'],
		                ['view', 'sort']
	                ]
        },
        handlers: {
            select: function (event, elfinderInstance) {
                if (event.data.selected.length == 1) {
                    var item = $('#' + event.data.selected[0]);
                    if (!item.hasClass('directory')) {
                        selectedFile = event.data.selected[0];
                        $('#elfinder-selectFile').show();
                        return;
                    }
                }
                $('#elfinder-selectFile').hide();
                selectedFile = null;
            }
        },
        width:950,
        height: 657,
        resizable:false
    };
    $('#elfinder').elfinder(options).elfinder('instance');
    $('.elfinder-toolbar:first').append('<div class="ui-widget-content ui-corner-all elfinder-buttonset" id="elfinder-selectFile" style="display:none; float:right;">' +
            '<div class="ui-state-default elfinder-button" title="Select" style="width: 100px;"></d iv>');
    $('#elfinder-selectFile').on("click", function (evt) {
        if (selectedFile != null) {
            $.post('connector/selectFile', { target: selectedFile }, $.fn.InsertAsset);
        }
    });
    $('#elfinder-selectFile').text(cap_SelectButton);
    $('#elfinder-selectFile').button();
    $('#elfinder-selectFile').css("height", "23px");
    $('#elfinder-selectFile').css("text-size", "8pt");
    $('#elfinder-selectFile').css("padding-bottom", "1px");
});
$.fn.isInIFrame = function () {
    return window.location != window.parent.location
}

$.fn.InsertAsset = function (selectedAsset) {
    var win = tinyMCEPopup.getWindowArg("window");
    win.document.getElementById(tinyMCEPopup.getWindowArg("input")).value = selectedAsset;
    // are we an image browser
    if (typeof (win.ImageDialog) != "undefined") {
        // we are, so update image dimensions...
        if (win.ImageDialog.getImageData)
            win.ImageDialog.getImageData();

        // ... and preview if necessary
        if (win.ImageDialog.showPreviewImage)
            win.ImageDialog.showPreviewImage(selectedAsset);
    }
    // close popup window
    tinyMCEPopup.close();
    return false;
}